package top.hidoctor.auscultation.feature.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.PatientBean;
import top.hidoctor.auscultation.bean.PatientsBean;
import top.hidoctor.auscultation.feature.fragment.other.PatientYbFragment;
import top.hidoctor.auscultation.feature.fragment.other.PatientDetailFragment;
import top.hidoctor.auscultation.net.c.PatientDetailContract;
import top.hidoctor.auscultation.net.p.PatientDetailPresenter;

public class PatientActivity extends BaseActivity implements View.OnClickListener, PatientDetailContract.View {
    private FrameLayout frameContent;
    private RadioGroup rgGroup;
    private RadioButton rbContent, rbPatient;
    private TextView tvSetting;
    private LinearLayout llHeader, llYangben;
    private EditText editSearch;
    private int[] resId = new int[]{R.id.rb_patient, R.id.rb_content};
    private ArrayList<Fragment> fragments;
    private int position = 0;
    private FragmentTransaction ft;
    private Fragment CURRENT_FRAGMENT;
    int tab_number = 0;
    private LinearLayout.LayoutParams linearParams;
    private PatientDetailFragment detailFrag;
    private PatientYbFragment ybFrag;
    private boolean isEdit = false;
    private PatientDetailPresenter presenter;
    private PatientBean bean;
    private PatientsBean.ResultBean info;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_patient);
        presenter = new PatientDetailPresenter(this, this);
        info = (PatientsBean.ResultBean) getIntent().getSerializableExtra("info");
        presenter.getDetail(info.getId());
        initView();

    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        llHeader = findViewById(R.id.ll_header);
        editSearch = findViewById(R.id.edit_search);
        llYangben = findViewById(R.id.ll_yangben);
        tvSetting = findViewById(R.id.tv_setting);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        frameContent = findViewById(R.id.frame_content);
        rgGroup = findViewById(R.id.rg_group);
        rbContent = findViewById(R.id.rb_content);
        rbPatient = findViewById(R.id.rb_patient);
        rbPatient.setOnClickListener(this);
        rbContent.setOnClickListener(this);
        tvSetting.setOnClickListener(this);

    }

    private void initEvent() {

        fragments = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putSerializable("info", bean);
        detailFrag = new PatientDetailFragment();
        ybFrag = new PatientYbFragment();
        detailFrag.setArguments(bundle);
        ybFrag.setArguments(bundle);
        fragments.add(0, detailFrag);
        fragments.add(1, ybFrag);
        rgGroup.check(resId[position]);
        switchFragment(tab_number);
    }

    private void switchFragment(int pos) {
        position = pos;
        rgGroup.check(resId[position]);
        ft = getSupportFragmentManager().beginTransaction();
        if (CURRENT_FRAGMENT != null) {
            ft.hide(CURRENT_FRAGMENT);
        }
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragments.get(pos).getClass().getName());
        if (fragment == null) {
            fragment = fragments.get(pos);
        }
        CURRENT_FRAGMENT = fragment;
        if (!fragment.isAdded()) {
            ft.add(R.id.frame_content, fragment, fragment.getClass().getName());
        } else {
            ft.show(fragment);
        }
        ft.commitAllowingStateLoss();
    }

    public void setInitView() {
        if (tab_number == 0) {
            rbPatient.setChecked(true);
        }
        if (tab_number == 1) {
            rbContent.setChecked(true);
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_patient:
                tab_number = 0;
                tvSetting.setVisibility(View.VISIBLE);
                switchFragment(0);
                break;
            case R.id.rb_content:
                tab_number = 1;
                tvSetting.setVisibility(View.GONE);
                switchFragment(1);
                break;
            case R.id.tv_setting:
                isEdit = !isEdit;
                tvSetting.setText(isEdit ? "取消" : "编辑");
                detailFrag.setEdit(isEdit);
                break;
        }

    }

    @Override
    public void getDetailScu(PatientBean bean) {
        this.bean = bean;
        initEvent();
    }

    @Override
    public void getUpadatDetailScu() {

    }

    @Override
    public void addPatientSuc() {

    }

    @Override
    public void getFileScu(String data) {

    }

    @Override
    public void onError(String code, String msg) {
        super.onError(code, msg);
        initEvent();
    }
}
